package top.frsf.object.plant;

import lombok.extern.slf4j.Slf4j;
import top.frsf.constants.ImageConstant;
import top.frsf.enums.BulletTypeEnum;
import top.frsf.factory.BulletFactory;
import top.frsf.object.GameObject;
import top.frsf.object.zombie.ZomObject;
import top.frsf.starter.impl.BulletStarter;
import top.frsf.starter.impl.ZomStarter;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;

import static top.frsf.panel.GamePanel.starterContext;

/**
 * 双发射手
 *
 * @author He Kuntao
 * @since 2025/8/6 14:54
 */
@Slf4j
public class RepeaterPea extends PlantObject {
    /**
     * 动画帧列表
     */
    private static final Image[] imageList = new Image[13];

    static {
        try {
            for (int i = 0; i < imageList.length; i++) {
                String imageUrl = String.format(ImageConstant.Plant.REPEATER_PEA, i);
                imageList[i] = ImageIO.read(new File(imageUrl));
            }
        } catch (Exception e) {
            log.error("图片加载失败", e);
            throw new RuntimeException(e);
        }
    }

    public RepeaterPea(int x, int y) {
        super(ImageConstant.Plant.REPEATER_PEA, x, y, 70, 70, 80, BulletTypeEnum.PEA_BULLET);
        addAnimation(ImageConstant.Plant.REPEATER_PEA, imageList);
        switchAnimation(ImageConstant.Plant.REPEATER_PEA, 100, true);
        setAttackCd(1500);
    }

    @Override
    public void performAttack(GameObject target) {
        ZomStarter zomStarter = starterContext.getStarter(ZomStarter.class);
        BulletStarter bulletStarter = starterContext.getStarter(BulletStarter.class);
        for (ZomObject zomObject : zomStarter.getZomList()) {
            double abs = Math.abs(getRectangle().getCenterY() - zomObject.getRectangle().getCenterY());
            // 是否同一行存在zom
            if (abs < 30) {
                bulletStarter.pesBulls.add(BulletFactory.createBullet(bulletType, getX() + getWidth() + 18, getY() + 5));
                bulletStarter.pesBulls.add(BulletFactory.createBullet(bulletType, getX() + getWidth() - 8, getY() + 5));
                break;
            }
        }
    }
}
